package com.mlink.ai.chat.ui.activity

import android.content.Intent
import android.view.View
import androidx.core.view.isVisible
import com.mlink.ai.chat.AiChatApplication
import com.mlink.ai.chat.assistant.robot.R
import com.mlink.ai.chat.assistant.robot.databinding.ActivitySettingsBinding
import com.mlink.ai.chat.constants.COMMUNITY_GUIDE_LINES_URL
import com.mlink.ai.chat.constants.MYSELF_URL_IN_GOOGLE_PLAY
import com.mlink.ai.chat.constants.PRIVACY_POLICY_URL
import com.mlink.ai.chat.constants.TERMS_OF_SERVICE_URL
import com.mlink.ai.chat.utils.AnalyticsParams
import com.mlink.ai.chat.utils.manageSubscription
import com.mlink.ai.chat.utils.openUrl
import com.mlink.ai.chat.utils.searchMyselfOnPlayStore
import com.mlink.ai.chat.utils.shareText

class SettingsActivity : BaseActivity<ActivitySettingsBinding>() {

    private val appViewModel = AiChatApplication.app.appViewModel

    private var productId: String? = null

    override fun getViewBinding(): ActivitySettingsBinding {
        return ActivitySettingsBinding.inflate(layoutInflater)
    }

    override fun initViews() {
        binding.backIv.setOnClickListener {
            finish()
        }

        appViewModel.getSubscribeLiveData().observe(this) {
            binding.subEntry.llRemoveLimits.visibility = if (it) View.GONE else View.VISIBLE
            binding.manageSubscriptionItem.root.visibility = if (it) View.VISIBLE else View.GONE
            binding.divider6.isVisible = it
        }

        appViewModel.purchaseLiveData.observe(this) {
            productId = it.first
        }

        binding.subEntry.llRemoveLimits.setOnClickListener {
            startActivity(Intent(this, BillingActivity::class.java)
                .apply {
                    putExtra(BillingActivity.KEY_VIP_TYPE, AnalyticsParams.SETTINGS)
                })
        }

        binding.rateUsItem.title.text = resources.getString(R.string.rate_us)
        binding.rateUsItem.icon.setImageResource(R.drawable.ic_set_list_rate)
        binding.rateUsItem.root.setOnClickListener {
            searchMyselfOnPlayStore()
        }

        binding.shareAppItem.title.text = resources.getString(R.string.share_the_app)
        binding.shareAppItem.icon.setImageResource(R.drawable.ic_set_list_share)
        binding.shareAppItem.root.setOnClickListener {
            doShareApp()
        }

        binding.manageSubscriptionItem.title.text = resources.getString(R.string.manage_subscription)
        binding.manageSubscriptionItem.icon.setImageResource(R.drawable.ic_set_list_manage)
        binding.manageSubscriptionItem.root.setOnClickListener {
           productId?.let { productId->
               manageSubscription(productId)
           }
        }

        binding.setLanguageItem.title.text = resources.getString(R.string.language_title)
        binding.setLanguageItem.icon.setImageResource(R.drawable.ic_set_list_language)
        binding.setLanguageItem.root.setOnClickListener {
            val intent = Intent(this, LanguageActivity::class.java)
                .putExtra(LanguageActivity.KEY_IS_NEED_RELAUNCH, true)

            startActivity(intent)
        }

        binding.setFeedbackItem.title.text = resources.getString(R.string.feedback_title)
        binding.setFeedbackItem.icon.setImageResource(R.drawable.ic_set_list_feedback)
        binding.setFeedbackItem.root.setOnClickListener {
            val intent = Intent(this, FeedbackActivity::class.java)

            startActivity(intent)
        }

        binding.aboutChatItem.title.text = resources.getString(R.string.about_ai_chat)
        binding.aboutChatItem.icon.setImageResource(R.drawable.ic_set_list_about)
        binding.aboutChatItem.root.setOnClickListener {
            startActivity(Intent(this, AboutActivity::class.java))
        }

        binding.termsItem.title.text = resources.getString(R.string.terms_of_service)
        binding.termsItem.icon.setImageResource(R.drawable.ic_set_list_terms)
        binding.termsItem.root.setOnClickListener {
            openUrl(TERMS_OF_SERVICE_URL)
        }

        binding.privacyItem.title.text = resources.getString(R.string.privacy_policy)
        binding.privacyItem.icon.setImageResource(R.drawable.ic_set_list_privacy)
        binding.privacyItem.root.setOnClickListener {
            openUrl(PRIVACY_POLICY_URL)
        }

        binding.communityItem.title.text = resources.getString(R.string.community_guidelines)
        binding.communityItem.icon.setImageResource(R.drawable.ic_set_list_guidelines)
        binding.communityItem.root.setOnClickListener {
            openUrl(COMMUNITY_GUIDE_LINES_URL)
        }
    }

    private fun doShareApp() {
        val shareText = getString(R.string.share_app_text) + MYSELF_URL_IN_GOOGLE_PLAY
        shareText(shareText)
    }

}